package com.mvu.banana.common.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.Widget;
import com.mvu.banana.common.widget.ErrorDialogBox;

/**
 */
public abstract class CommonComposite<T extends ValueDTO> extends Composite{
  protected T dto;

  private CommonComposite() {
  }

  public CommonComposite(T dto){
    this.dto = dto;
  }

  public void initWidget(Widget widget) {
    dto.prepare(this);
    super.initWidget(widget);
  }

  public void post(FocusWidget trigger) {
    dto.post(this, trigger);
  }

  public void post(FocusWidget trigger, AsyncCallback<? extends T> callback) {
    dto.post(this, trigger, callback);
  }

  public void get() {
    dto.get(this);
  }

  public void get(AsyncCallback<? extends T> callback) {
    dto.get(callback);
  }

  public void highlight(Widget input, boolean valid) {
    String invalid = GlobalResources.get.css().invalid();
    if (valid) {
      input.removeStyleName(invalid);
    } else {
      input.addStyleName(invalid);
    }
  }

  protected void showError(String messageKey) {
    ErrorDialogBox errorDialogBox = new ErrorDialogBox();
    errorDialogBox.setMessage(CommonConstants.get.getString(messageKey));
    errorDialogBox.show();
  }

  public String getStringValue(Enum s) {
    return CommonConstants.get.getString(s.name());
  }
}
